# -*- coding: utf-8 -*-
from datetime import timedelta

from utils.operators.spark_submit import SparkSubmitOperator

from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dwd.tab.dwd_tab_barscan_sitesend_base_dt import jms_dwd__dwd_tab_barscan_sitesend_base_dt
from jms.dwd.tab.dwd_tab_reback_transfer_express_base import jms_dwd__dwd_tab_reback_transfer_express_base
from jms.dwd.oms.dwd_yl_oms_interceptorrecord_base_dt import jms_dwd__dwd_yl_oms_interceptorrecord_base_dt
from jms.dwd.tab.dwd_tab_barscan_arrival_base_dt import jms_dwd__dwd_tab_barscan_arrival_base_dt
from jms.dwd.tab.dwd_tab_barscan_centersend_base_dt import jms_dwd__dwd_tab_barscan_centersend_base_dt
from jms.dwd.tab.dwd_tab_barscan_collect_base_dt import jms_dwd__dwd_tab_barscan_collect_base_dt
from jms.dwd.tab.dwd_tab_barscan_sign_base_dt import jms_dwd__dwd_tab_barscan_sign_base_dt
from jms.dwd.tab.dwd_tab_barscan_sitearrival_base_dt import jms_dwd__dwd_tab_barscan_sitearrival_base_dt
from jms.dim.tms.dim_tab_tmsnew_time_effective_base_dt import jms_dim__dim_tab_tmsnew_time_effective_base_dt
from jms.dwd.tms.dwd_tmsnew_shipment_stop_union_base_dt import jms_dwd__dwd_tmsnew_shipment_stop_union_base_dt
from jms.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
from jms.time_sensor.time_after_03_45 import time_after_03_45

jms_dm__dm_collect_analysis_detail_mid_dt = SparkSubmitOperator(
    task_id='jms_dm__dm_collect_analysis_detail_mid_dt',
    email=['guoruiling@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool_slots=4,
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_dm__dm_collect_analysis_detail_mid_dt_{{ execution_date | date_add(1) | cst_ds }}',  # yarn 任务名称
    driver_memory='20G',
    driver_cores=10,
    executor_cores=6,
    executor_memory='12G',
    num_executors=30,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 60,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 500,
          'spark.default.paralleism': 500,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.network.timeout' : 200, #网络交互超时时间 默认120s
          },
    jars='hdfs:///scheduler/jms/spark/zyt/collect_analysisi/common-1.0-SNAPSHOT.jar',
    application='hdfs:///scheduler/jms/spark/zyt/collect_analysisi/CollectAnalysisi.jar',
    java_class='com.yunlu.bigdata.jobs.route.export.CollectTimeAnalysisMid',  # spark 主类
    application_args=['{{ execution_date | cst_ds }}'],  # 参数dt
    #execution_timeout=timedelta(minutes=180)
    #excel平均时长:26分46秒
    #execution_timeout = timedelta(minutes=60)
    #excel平均时长:26分46秒
    execution_timeout = timedelta(minutes=120),
    retries=1,
)

# 设置依赖
jms_dm__dm_collect_analysis_detail_mid_dt << [
    jms_dwd__dwd_tab_barscan_centersend_base_dt,
    jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
    jms_dim__dim_tab_tmsnew_time_effective_base_dt,
    jms_dwd__dwd_yl_oms_interceptorrecord_base_dt,
    jms_dwd__dwd_tab_barscan_sitearrival_base_dt,
    jms_dwd__dwd_tab_barscan_sitesend_base_dt,
    jms_dwd__dwd_tab_barscan_sign_base_dt,
    jms_dwd__dwd_tab_barscan_collect_base_dt,
    jms_dwd__dwd_tab_barscan_arrival_base_dt,
    jms_dwd__dwd_tab_reback_transfer_express_base,
    jms_dwd__dwd_tmsnew_shipment_stop_union_base_dt,
    jms_dim__dim_network_whole_massage,
    time_after_03_45
]
